﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.Model;
using Microsoft.VisualBasic;
using MSBTC.ReportViewer;

namespace MSBTC.WinForms
{
    public partial class MaterialEstimationForm : Form
    {
        private string Datamanip;

        MDIParent mdipar = new MDIParent();
        PMSContext dbcontext = new PMSContext();
        Material mtrial = new Material();
        FormHelpers help = new FormHelpers();
        private int rowselect;
        public MaterialEstimationForm()
        {
            InitializeComponent();
        }

        private void MaterialForm_Load(object sender, EventArgs e)
        {

            cmbPname.Focus();
            cmbPname.TabIndex = 1;
            cmbMname.TabIndex = 2;
            cmbCategory.TabIndex = 3;
            cmbUnit.TabIndex = 4;
            txtQty.TabIndex = 5;
            txtDCost.TabIndex = 6;
            txtUCost.TabIndex = 7;
            groupBox2.TabIndex = 8;
            groupBox1.TabIndex = 9;
            //txtSearch.TabIndex = 10;
            btnClose.TabIndex = 11;






            txtDCost.Enabled = false;
            txtTotCost.Enabled = false;
            txtValue.Enabled = false;
            txtVAT.Enabled = false;
            txtTotInCost.Enabled = false;


            var matinv = (from m in dbcontext.MaterialsInventorys
                          select m).ToList();
            if (matinv.Count == 0)
            {
                MessageBox.Show("Cannot retrieve records from MaterialsInventory_TB", "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                foreach (var mt in matinv)
                {
                    cmbMname.Items.Add(mt.Material_Name);
                }
            }

            var prj = (from pj in dbcontext.Projects
                       select pj).ToList();
            if (prj.Count == 0)
            {
                MessageBox.Show("Cannot retrieve records from Project_TB", "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                foreach (var pj in prj)
                {
                    cmbPname.Items.Add(pj.Project_Name);
                }
            }

            var cat = (from c in dbcontext.Categories
                       select c).ToList();
            if (cat.Count == 0)
            {
                MessageBox.Show("Cannot retrieve records from Category_TB", "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                foreach (var ct in cat)
                {
                    cmbCategory.Items.Add(ct.Category_Name);
                }
            }
            RefreshFields();

            RefreshDB();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        #region functions
        private void RefreshDB()
        {
            //retrieve all data

            //var cf = (from x in dbcontext.Projects
            //          where x.Project_Name == cmbPname.Text
            //          select x).SingleOrDefault();


            //var mat = (from m in dbcontext.Materials
            //           where m.Projectid_FK == cf.Projectid_PK
            //           select m).ToList();

            //LVMatInfo.Items.Clear();
            //if (mat != null)
            //{

            //    foreach (var sqlMat in mat)
            //    {
            //        //add items in listview box
            //        ListViewItem lvitems = LVMatInfo.Items.Add(sqlMat.Materialsid_PK.ToString());

            //        //lvitems.SubItems.Add(sqlMat.Materialsid_PK.ToString());

            //        //materialinv name
            //        var matinv = (from pj in dbcontext.MaterialsInventorys
            //                      where pj.MaterialsInventoryid_PK == sqlMat.MaterialsInventoryid_FK
            //                      select pj).SingleOrDefault();
            //        if (matinv != null)
            //        {
            //            lvitems.SubItems.Add(matinv.Material_Name.ToString());
            //        }
            //        //projects name
            //        var prj = (from pj in dbcontext.Projects
            //                   where pj.Projectid_PK == sqlMat.Projectid_FK
            //                   select pj).SingleOrDefault();
            //        if (prj != null)
            //        {
            //            lvitems.SubItems.Add(prj.Project_Name.ToString());
            //        }
            //        //categories name
            //        var cat = (from ct in dbcontext.Categories
            //                   where ct.Categoryid_PK == sqlMat.Categoryid_FK
            //                   select ct).SingleOrDefault();
            //        if (cat != null)
            //        {
            //            lvitems.SubItems.Add(cat.Category_Name.ToString());
            //        }

            //        lvitems.SubItems.Add(sqlMat.Unit.ToString());
            //        lvitems.SubItems.Add(sqlMat.Quantity.ToString());
            //        lvitems.SubItems.Add(sqlMat.Direct_Cost.ToString());
            //        lvitems.SubItems.Add(sqlMat.TotMarkup_pct.ToString());
            //        lvitems.SubItems.Add(sqlMat.TotMarkup_Value.ToString());
            //        lvitems.SubItems.Add(sqlMat.Markupin_OCM.ToString());
            //        lvitems.SubItems.Add(sqlMat.Markupin_Profit.ToString());
            //        lvitems.SubItems.Add(sqlMat.VAT.ToString());

            //        lvitems.SubItems.Add(sqlMat.TotIndirectcost.ToString());
            //        lvitems.SubItems.Add(sqlMat.Tot_Cost.ToString());
            //        lvitems.SubItems.Add(sqlMat.Unit_Cost.ToString());
            //    }
            //}
            this.Refresh();
        }

        private void ClearFields()
        {
            btnNew.Enabled = true;

            txtID.Clear();
            cmbMname.SelectedIndex = 0;
            cmbPname.SelectedIndex = 0;
            cmbCategory.SelectedIndex = 0;
            cmbUnit.SelectedIndex = 0;
            
            txtQty.Clear();
            txtDCost.Clear();
            txtUCost.Clear();
            txtPct.Clear();
         
            txtVAT.Clear();
            txtValue.Clear();
            txtTotCost.Clear();
            txtTotInCost.Clear();
            txtProfit.Clear();
            txtOCM.Clear();
       
            //txtSearch.Clear();
        
        }

        private void DisableFields()
        {
            txtID.Enabled = false;
            cmbMname.Enabled = false;
            //cmbPname.Enabled = false;
            cmbCategory.Enabled = false;
            cmbUnit.Enabled = false;
            txtQty.Enabled = false;
            txtDCost.Enabled = false;
            txtVAT.Enabled = false;
            txtPct.Enabled = false;
            txtValue.Enabled = false;
            txtOCM.Enabled = false;
            txtProfit.Enabled = false;
            txtTotCost.Enabled = false;
            txtTotInCost.Enabled = false;
            txtUCost.Enabled = false;

        }

        private void EnableFields()
        {
            txtID.Enabled = true;
            cmbMname.Enabled = true;
            cmbPname.Enabled = true;
            cmbCategory.Enabled = true;
            cmbUnit.Enabled = true;
            txtQty.Enabled = true;
            txtDCost.Enabled = true;
            //txtVAT.Enabled = true;
            txtPct.Enabled = true;
            //txtValue.Enabled = true;
            txtOCM.Enabled = true;
            txtProfit.Enabled = true;
            //txtTotCost.Enabled = true;
            //txtTotInCost.Enabled = true;
            txtUCost.Enabled = true;

            //txtValue.Text = "0";
            //txtVAT.Text = "0";
            //txtTotCost.Text = "0";
            //txtTotInCost.Text = "0";
        }

        private void RefreshFields()
        {
            DisableFields();
            ClearFields();
            RefreshDB();

            btnNew.Enabled = true;
            btnDel.Enabled = false;
            btnEdit.Enabled = false;
            btnClear.Enabled = true;
            btnSave.Enabled = false;
        }

        private void TotalCostFunc()
        {


            var x = double.Parse(txtQty.Text) * double.Parse(txtUCost.Text);
            txtTotCost.Text = Convert.ToString(x);


        }



        private void ValueFunc()
        {

            var x = ((double.Parse(txtDCost.Text) * double.Parse(txtPct.Text)) / 100);
            txtValue.Text = Convert.ToString(x);


        }
        private void VatFunc()
        {

            var x = double.Parse(txtTotCost.Text) - double.Parse(txtDCost.Text) - double.Parse(txtValue.Text);
            txtVAT.Text = Convert.ToString(x);

        }

        private void IndCostFunc()
        {

            var x = double.Parse(txtValue.Text) + double.Parse(txtVAT.Text);
            txtTotInCost.Text = Convert.ToString(x);

        }


        #endregion

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearFields();
            DisableFields();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            LVMatInfo.Items.Clear();
            Datamanip = "Add";

            EnableFields();
            ClearFields();

            btnSave.Enabled = true;
            btnNew.Enabled = false;
            LVMatInfo.Enabled = false;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Datamanip = "Edit";

            EnableFields();

            btnSave.Enabled = true;
            btnNew.Enabled = false;
            btnDel.Enabled = false;
            btnEdit.Enabled = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {


            if ((txtQty.Text == "") || (txtUCost.Text == "") || (txtPct.Text == "") || (txtPct.Text == "") || (txtOCM.Text == "") || (txtProfit.Text == ""))
            {
                MessageBox.Show("Some fields were not filled up", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if ((cmbPname.SelectedIndex == 0) || (cmbCategory.SelectedIndex == 0) || (cmbUnit.SelectedIndex == 0))
            {
                MessageBox.Show("Please Select one in Project, Category and Unit of measure", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if ((cmbUnit.SelectedIndex == 0 || cmbUnit.SelectedIndex == 4 || cmbUnit.SelectedIndex == 18 || cmbUnit.SelectedIndex == 24))
            {
                MessageBox.Show("Please fill up the Unit", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {

                if (Datamanip == "Add")
                {
                    var matinv = (from m in dbcontext.MaterialsInventorys
                                  where m.Material_Name == cmbMname.SelectedItem
                                  select m).SingleOrDefault();

                    if (matinv == null)
                    {
                        MessageBox.Show("Cannot retrieve MaterialsInventorys_PK", "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    var prj = (from pj in dbcontext.Projects
                               where pj.Project_Name == cmbPname.SelectedItem
                               select pj).SingleOrDefault();
                    if (prj == null)
                    {
                        MessageBox.Show("Cannot retrieve Projectid_PK", "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    var cat = (from ct in dbcontext.Categories
                               where ct.Category_Name == cmbCategory.SelectedItem
                               select ct).SingleOrDefault();
                    if (cat == null)
                    {
                        MessageBox.Show("Cannot retrieve Categoryid_PK", "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }



                    var mat = (from m in dbcontext.Materials
                               where m.MaterialsInventoryid_FK == matinv.MaterialsInventoryid_PK && m.Projectid_FK == prj.Projectid_PK
                               && m.Categoryid_FK == cat.Categoryid_PK
                               select m).SingleOrDefault();

                    if (mat == null)
                    {
                        mtrial.MaterialsInventoryid_FK = matinv.MaterialsInventoryid_PK;
                        mtrial.Projectid_FK = prj.Projectid_PK;
                        mtrial.Categoryid_FK = cat.Categoryid_PK;
                        var x = cmbUnit.Text;
                        mtrial.Unit = x;
                        mtrial.Quantity = Convert.ToInt32(txtQty.Text);
                        mtrial.Direct_Cost = txtDCost.Text;
                        //var a = double.Parse(txtVAT.Text);
                        mtrial.VAT = txtVAT.Text;
                        mtrial.Unit_Cost = txtUCost.Text;
                        mtrial.TotIndirectcost = txtTotInCost.Text;
                        mtrial.Tot_Cost = txtTotCost.Text;
                        mtrial.TotMarkup_pct = txtPct.Text;
                        //var b = double.Parse(txtValue.Text);
                        mtrial.TotMarkup_Value = txtValue.Text;
                        mtrial.Markupin_OCM = txtOCM.Text;
                        mtrial.Markupin_Profit = txtProfit.Text;

                        dbcontext.Entry(mtrial).State = EntityState.Added;
                        dbcontext.SaveChanges();

                        MessageBox.Show("Successfully Saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();
                        RefreshDB();
                        
                    }
                    LVMatInfo.Enabled = true;
                }



                else if (Datamanip == "Edit")
                {
                    DialogResult YN = MessageBox.Show("Do you want to replace it?", "Modify", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (YN == DialogResult.Yes)
                    {


                        var replace = dbcontext.Materials.Find(Convert.ToInt32(txtID.Text));


                        var mname = (from c in dbcontext.MaterialsInventorys
                                     where c.Material_Name == cmbMname.SelectedItem
                                     select c).SingleOrDefault();
                        replace.MaterialsInventoryid_FK = mname.MaterialsInventoryid_PK;

                        var pname = (from c in dbcontext.Projects
                                     where c.Project_Name == cmbPname.SelectedItem
                                     select c).SingleOrDefault();
                        replace.Projectid_FK = pname.Projectid_PK;

                        var cname = (from c in dbcontext.Categories
                                     where c.Category_Name == cmbCategory.SelectedItem
                                     select c).SingleOrDefault();
                        replace.Categoryid_FK = cname.Categoryid_PK;

                        replace.Unit = cmbUnit.Text;
                        replace.Quantity = Convert.ToInt32(txtQty.Text);
                        replace.Direct_Cost = txtDCost.Text;
                        //var a = double.Parse(txtVAT.Text);
                        replace.VAT = txtVAT.Text;
                        replace.Unit_Cost = txtUCost.Text;
                        replace.TotIndirectcost = txtTotInCost.Text;
                        replace.Tot_Cost = txtTotCost.Text;
                        replace.TotMarkup_pct = txtPct.Text;
                        //var b = double.Parse(txtValue.Text);
                        replace.TotMarkup_Value = txtValue.Text;
                        replace.Markupin_OCM = txtOCM.Text;
                        replace.Markupin_Profit = txtProfit.Text;



                        dbcontext.Entry(replace).State = EntityState.Modified;
                        dbcontext.SaveChanges();

                        MessageBox.Show("Successfully Modified.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();
                        RefreshDB();
                    }
                }


            }
        }

        private void cmbMname_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void txtTotCost_TextChanged(object sender, EventArgs e)
        {
        }








        private void txtPct_TextChanged_1(object sender, EventArgs e)
        {


            //##########################################################################



            if (txtPct.Text == "")
            {
                //txtTotCost.Text = txtUCost.Text;

                if (txtDCost.Text == "")
                {
                    txtValue.Text = txtPct.Text;
                    return;
                }
                else
                {
                    var x = ((1 * double.Parse(txtDCost.Text)) / 100);
                    txtValue.Text = Convert.ToString(x);
                    return;
                }

            }

            if ((Information.IsNumeric(txtPct.Text) == false))
            {

                MessageBox.Show("Please enter numbers.");
                txtPct.Clear();

                //    TotalCostFunc();
                //}
            }
            else
            {
                if (txtDCost.Text == "")
                {

                    var x = (double.Parse(txtPct.Text) * 1 / 100);
                    txtValue.Text = Convert.ToString(x);


                }

                else
                {

                    var x = ((double.Parse(txtDCost.Text) * double.Parse(txtPct.Text)) / 100);
                    txtValue.Text = Convert.ToString(x);


                }
            }

            //######################################################################################3


            if (txtQty.Text != "" && txtDCost.Text != "" && txtUCost.Text != "" && txtPct.Text != "")
            {
                //vat
                var x = double.Parse(txtTotCost.Text) - double.Parse(txtDCost.Text) - double.Parse(txtValue.Text);
                txtVAT.Text = Convert.ToString(x);

                // totIndirect Cost
                var a = double.Parse(txtValue.Text) + double.Parse(txtVAT.Text);
                txtTotInCost.Text = Convert.ToString(a);

            }
            else
            {
                txtVAT.Clear();
                txtTotInCost.Clear();


            }






        }

        private void txtQty_TextChanged(object sender, EventArgs e)
        {






            //######################################################################################################



            if (txtQty.Text == "")
            {
                txtTotCost.Text = txtUCost.Text;
                return;
            }



            if ((Information.IsNumeric(txtQty.Text) == false))
            {

                MessageBox.Show("Please enter numbers.");
                txtQty.Clear();

                //    TotalCostFunc();
                //}
            }
            else
            {
                if (txtUCost.Text == "")
                {

                    var x = double.Parse(txtQty.Text) * 1;
                    txtTotCost.Text = Convert.ToString(x);


                }

                else
                {


                    var x = double.Parse(txtQty.Text) * double.Parse(txtUCost.Text);
                    txtTotCost.Text = Convert.ToString(x);


                }
            }


            ////#################################################################################################333


            if (txtQty.Text != "" && txtDCost.Text != "" && txtUCost.Text != "" && txtPct.Text != "")
            {
                //vat
                var x = double.Parse(txtTotCost.Text) - double.Parse(txtDCost.Text) - double.Parse(txtValue.Text);
                txtVAT.Text = Convert.ToString(x);
                // totIndirect Cost
                var a = double.Parse(txtValue.Text) + double.Parse(txtVAT.Text);
                txtTotInCost.Text = Convert.ToString(a);

            }
            else
            {
                txtVAT.Clear();
                txtTotInCost.Clear();


            }


        }

        private void txtDCost_TextChanged(object sender, EventArgs e)
        {




            ///##############################################################################################################



            if (txtDCost.Text == "")
            {
                //txtTotCost.Text = txtUCost.Text;




                if (txtPct.Text == "")
                {



                    txtValue.Text = txtDCost.Text;

                    return;
                }
                else
                {
                    var x = ((1 * double.Parse(txtPct.Text)) / 100);
                    txtValue.Text = Convert.ToString(x);


                    //var y = double.Parse(txtTotCost.Text) - 0 - double.Parse(txtValue.Text);
                    //txtVAT.Text = Convert.ToString(y);


                    return;

                }

            }

            if ((Information.IsNumeric(txtDCost.Text) == false))
            {

                MessageBox.Show("Please enter numbers.");
                txtDCost.Clear();

                //    TotalCostFunc();
                //}
            }
            else
            {
                if (txtPct.Text == "")
                {

                    var x = (double.Parse(txtDCost.Text) * 1 / 100);
                    txtValue.Text = Convert.ToString(x);




                }

                else
                {
                    //value
                    var x = ((double.Parse(txtDCost.Text) * double.Parse(txtPct.Text)) / 100);
                    txtValue.Text = Convert.ToString(x);





                }

            }



            if (txtQty.Text != "" && txtDCost.Text != "" && txtUCost.Text != "" && txtPct.Text != "")
            {
                //vat
                var x = double.Parse(txtTotCost.Text) - double.Parse(txtDCost.Text) - double.Parse(txtValue.Text);
                txtVAT.Text = Convert.ToString(x);
                // totIndirect Cost
                var a = double.Parse(txtValue.Text) + double.Parse(txtVAT.Text);
                txtTotInCost.Text = Convert.ToString(a);

            }
            else
            {
                txtVAT.Clear();
                txtTotInCost.Clear();


            }




        }

        private void txtUCost_TextChanged(object sender, EventArgs e)
        {



            //###########################################################################################3     




            if (txtUCost.Text == "")
            {
                txtTotCost.Text = txtQty.Text;
                return;
            }



            if ((Information.IsNumeric(txtUCost.Text) == false))
            {

                MessageBox.Show("Please enter numbers.");
                txtUCost.Clear();

                //    TotalCostFunc();
                //}
            }
            else
            {
                if (txtQty.Text == "")
                {

                    var x = double.Parse(txtUCost.Text) * 1;
                    txtTotCost.Text = Convert.ToString(x);


                }

                else
                {


                    var x = double.Parse(txtQty.Text) * double.Parse(txtUCost.Text);
                    txtTotCost.Text = Convert.ToString(x);


                }
            }




            //##############################################################################################################

            if (txtQty.Text != "" && txtDCost.Text != "" && txtUCost.Text != "" && txtPct.Text != "")
            {
                //vat
                var x = double.Parse(txtTotCost.Text) - double.Parse(txtDCost.Text) - double.Parse(txtValue.Text);
                txtVAT.Text = Convert.ToString(x);
                // totIndirect Cost
                var a = double.Parse(txtValue.Text) + double.Parse(txtVAT.Text);
                txtTotInCost.Text = Convert.ToString(a);

            }
            else
            {
                txtVAT.Clear();
                txtTotInCost.Clear();


            }








        }

        private void LVMatInfo_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            //focus the selected items
            rowselect = LVMatInfo.FocusedItem.Index;


            //getting the information from the listview to textbox
            txtID.Text = LVMatInfo.Items[rowselect].SubItems[0].Text;
            cmbMname.Text = LVMatInfo.Items[rowselect].SubItems[1].Text;
            cmbPname.Text = LVMatInfo.Items[rowselect].SubItems[2].Text;
            cmbCategory.Text = LVMatInfo.Items[rowselect].SubItems[3].Text;
            cmbUnit.SelectedItem = LVMatInfo.Items[rowselect].SubItems[4].Text;
            txtQty.Text = LVMatInfo.Items[rowselect].SubItems[5].Text;
            txtDCost.Text = LVMatInfo.Items[rowselect].SubItems[6].Text;
            txtUCost.Text = LVMatInfo.Items[rowselect].SubItems[14].Text;
            txtPct.Text = LVMatInfo.Items[rowselect].SubItems[7].Text;
            txtOCM.Text = LVMatInfo.Items[rowselect].SubItems[9].Text;
            txtProfit.Text = LVMatInfo.Items[rowselect].SubItems[10].Text;

            //txtSearch.Enabled = false;
            btnDel.Enabled = true;
            btnEdit.Enabled = true;
            btnSave.Enabled = false;
            btnNew.Enabled = true;
        }



        private void btnDel_Click_1(object sender, EventArgs e)
        {
            DialogResult YN = MessageBox.Show("Do you want to Delete it?", "Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (YN == DialogResult.Yes)
            {
                var del = dbcontext.Materials.Find(Convert.ToInt32(txtID.Text));//get the same ID of purchase order to delete
                if (del != null)
                {
                    //delete the ID selected
                    dbcontext.Entry(del).State = EntityState.Deleted;
                    dbcontext.SaveChanges();
                }

                
                RefreshFields();
                //RefreshDB();
            }
        }

        private void toolStrip1_ItemClicked_1(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            help.GetPrintStat("nALL");
            help.getPname(cmbPname.SelectedItem.ToString());
            MatEstimationRptForm matrpt = new MatEstimationRptForm();

            matrpt.Show();
        }

        private void cmbPname_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            if (cmbPname.SelectedIndex == 0)
            {
                LVMatInfo.Items.Clear();
                return;
            }
            else
            {
                var cf = (from x in dbcontext.Projects
                          where x.Project_Name == cmbPname.Text
                          select x).SingleOrDefault();


                var mat = (from m in dbcontext.Materials
                           where m.Projectid_FK == cf.Projectid_PK
                           select m).ToList();

                LVMatInfo.Items.Clear();
                if (mat != null)
                {

                    foreach (var sqlMat in mat)
                    {
                        //add items in listview box
                        ListViewItem lvitems = LVMatInfo.Items.Add(sqlMat.Materialsid_PK.ToString());

                        //lvitems.SubItems.Add(sqlMat.Materialsid_PK.ToString());

                        //materialinv name
                        var matinv = (from pj in dbcontext.MaterialsInventorys
                                      where pj.MaterialsInventoryid_PK == sqlMat.MaterialsInventoryid_FK
                                      select pj).SingleOrDefault();
                        if (matinv != null)
                        {
                            lvitems.SubItems.Add(matinv.Material_Name.ToString());
                        }
                        //projects name
                        var prj = (from pj in dbcontext.Projects
                                   where pj.Projectid_PK == sqlMat.Projectid_FK
                                   select pj).SingleOrDefault();
                        if (prj != null)
                        {
                            lvitems.SubItems.Add(prj.Project_Name.ToString());
                        }
                        //categories name
                        var cat = (from ct in dbcontext.Categories
                                   where ct.Categoryid_PK == sqlMat.Categoryid_FK
                                   select ct).SingleOrDefault();
                        if (cat != null)
                        {
                            lvitems.SubItems.Add(cat.Category_Name.ToString());
                        }

                        lvitems.SubItems.Add(sqlMat.Unit.ToString());
                        lvitems.SubItems.Add(sqlMat.Quantity.ToString());
                        lvitems.SubItems.Add(sqlMat.Direct_Cost.ToString());
                        lvitems.SubItems.Add(sqlMat.TotMarkup_pct.ToString());
                        lvitems.SubItems.Add(sqlMat.TotMarkup_Value.ToString());
                        lvitems.SubItems.Add(sqlMat.Markupin_OCM.ToString());
                        lvitems.SubItems.Add(sqlMat.Markupin_Profit.ToString());
                        lvitems.SubItems.Add(sqlMat.VAT.ToString());

                        lvitems.SubItems.Add(sqlMat.TotIndirectcost.ToString());
                        lvitems.SubItems.Add(sqlMat.Tot_Cost.ToString());
                        lvitems.SubItems.Add(sqlMat.Unit_Cost.ToString());
                    }
                }
            }

        }

        private void txtOCM_TextChanged(object sender, EventArgs e)
        {
            if (txtOCM.Text == "")
            {
                return;
            }
        
            if ((Information.IsNumeric(txtOCM.Text) == false))
            {
                txtOCM.Clear();
                MessageBox.Show("Please enter numbers.");
                
            }
        }

        private void txtProfit_TextChanged(object sender, EventArgs e)
        {

            //help.GetPrintStat("ALL");

            //MatEstimationRptForm matrpt = new MatEstimationRptForm();

            if (txtProfit.Text == "")
            {
                return;
            }
            
            if ((Information.IsNumeric(txtProfit.Text) == false))
            {
                txtProfit.Clear();
                MessageBox.Show("Please enter numbers.");



            //matrpt.Show();

            }

        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            RefreshFields();
            LVMatInfo.Enabled = true;
        }


    }

}